package com.yiwei.zxp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BuyerOrderDetailActivity;
import com.yiwei.zxp.activity.LogisticsActivity;
import com.yiwei.zxp.activity.MyCommentActivity;
import com.yiwei.zxp.activity.PayTypeActivity;
import com.yiwei.zxp.activity.SendEvaluateActivity;
import com.yiwei.zxp.adapter.BuyerOrderAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.BuyerOrderDetailBean;
import com.yiwei.zxp.api.bean.BuyerOrderListBean;
import com.yiwei.zxp.api.model.BuyerOrderListModel;
import com.yiwei.zxp.api.model.ItemGoodsDetail;
import com.yiwei.zxp.event.BussinessOrderNeedRefreshEvent;
import com.yiwei.zxp.event.BuyerOrderNeedRefreshEvent;
import com.yiwei.zxp.event.PaySuccessEvent;
import com.yiwei.zxp.event.SelectQuickInfoEvent;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.OnLoadeMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-5-28.
 */

public class BuyerOrderFragment extends BaseFragment implements BuyerOrderAdapter.OnChooseListener {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    Unbinder unbinder;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private BuyerOrderAdapter adapter;
    private int id;
    private OnLoadeMoreListener listener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getBuyerOrder(page);
        }
    };
    private boolean needRefresh = false;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_buyer_order;
    }

    @Override
    protected void initView() {
        if (getArguments() != null) {
            id = getArguments().getInt("id", 0);
        }
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                listener.setPage(1);
                getBuyerOrder(1);
            }
        });
        adapter = new BuyerOrderAdapter(getContext());
        adapter.setOnChooseListener(this);
        list.addOnScrollListener(listener);
        list.setAdapter(adapter);
        EventBus.getDefault().register(this);
        listener.setPage(1);
        getBuyerOrder(1);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BuyerOrderNeedRefreshEvent event) {
        needRefresh = true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(PaySuccessEvent event) {
        needRefresh = true;
    }


    @Override
    public void onResume() {
        super.onResume();
        if (needRefresh) {
            listener.setPage(1);
            getBuyerOrder(1);
            needRefresh = false;
        }
    }


    public void getBuyerOrder(int page) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        BuyerOrderListBean bean = new BuyerOrderListBean();
        bean.page = page;
        bean.status = id;
        Api.api_service.getBuyerOrderList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    public void getBuyerOrderDelete(String order_no) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        BuyerOrderDetailBean bean = new BuyerOrderDetailBean();
        bean.order_no = order_no;
        Api.api_service.getBuyerOrderDelete(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(getContext(), "删除成功");
                    listener.setPage(1);
                    getBuyerOrder(1);
                });
    }

    public void getBuyerOrderSure(String order_no) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        BuyerOrderDetailBean bean = new BuyerOrderDetailBean();
        bean.order_no = order_no;
        Api.api_service.getOrderComplete(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(getContext(), "确认收货成功");
                    listener.setPage(1);
                    getBuyerOrder(1);
                });
    }


    @Override
    public void onPay(String order_no) {
        ArrayList<String> data = new ArrayList<>();
        data.add(order_no);
        Intent intent = new Intent(getActivity(), PayTypeActivity.class);
        intent.putStringArrayListExtra("data", data);
        startActivity(intent);
        getActivity().finish();
    }

    @Override
    public void onSendTips(String order_no) {
        ToastUtil.makeText(getContext(), "提醒成功");
    }

    @Override
    public void onSure(String order_no) {
        Dialog.showDefaultDialog(getContext(), "", "是否确认收货？", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {
                getBuyerOrderSure(order_no);

            }

            @Override
            public void cancel() {

            }
        });

    }

    @Override
    public void onWuliu(String order_no) {
        Intent intent = new Intent(getActivity(), LogisticsActivity.class);
        intent.putExtra("order_no", order_no);
        startActivity(intent);
    }

    @Override
    public void onDelete(String order_no) {
        Dialog.showDefaultDialog(getContext(), "", "是否确定删除该订单？", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {
                getBuyerOrderDelete(order_no);

            }

            @Override
            public void cancel() {

            }
        });

    }

    @Override
    public void onCall(String phone) {

    }

    @Override
    public void onComment(String order_no) {
        Intent intent = new Intent(getContext(), SendEvaluateActivity.class);
        intent.putExtra("order_no", order_no);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }

    @Override
    public void onWatchComment() {
        Util.startActivity(getContext(), MyCommentActivity.class);
    }


}
